#include <convert_manager.h>
#include <video_manager.h>
#include <config.h>
#include <stdint.h>
#include <string.h>
#include <stdlib.h>


static int Rgb2RgbisSupport(int iPixelFormatIn,int iPixelFormatOut)
{
    if(iPixelFormatIn != V4L2_PIX_FMT_RGB565)
    return -1;

    if((iPixelFormatOut != V4L2_PIX_FMT_RGB565) && (iPixelFormatOut != V4L2_PIX_FMT_RGB32))
        return -1;

    return 0;
}

static int Rgb2RgbConvert(PT_VideoBuf iVideoBufIn,PT_VideoBuf iVideoBufOut)
{
    int x,y;
    uint16_t color;
    int r,g,b;
    PT_PixelDatas ptPixelDatasIn = &iVideoBufIn->tPixelDatas;
    PT_PixelDatas ptPixelDatasOut = &iVideoBufOut->tPixelDatas;
    unsigned short *Ptr = (unsigned short *)ptPixelDatasIn->aucPixelDatas;  //这里将指针类型转换为了unsigned short类型，这样Ptr++就是指向下一个16位的数据，方便了后面的操作

    if(iVideoBufOut->iPixelFormat == V4L2_PIX_FMT_RGB565)
    {
        ptPixelDatasOut->iHeight = ptPixelDatasIn->iHeight;
        ptPixelDatasOut->iWidth = ptPixelDatasIn->iWidth;
        ptPixelDatasOut->iBpp = ptPixelDatasIn->iBpp;
        ptPixelDatasOut->iLineBytes = ptPixelDatasIn->iWidth * ptPixelDatasIn->iBpp / 8;
        ptPixelDatasOut->iTotalBytes = ptPixelDatasIn->iLineBytes * ptPixelDatasIn->iHeight;
        if(ptPixelDatasOut->aucPixelDatas == NULL)
        {
            ptPixelDatasOut->aucPixelDatas = (unsigned char *)malloc(ptPixelDatasOut->iTotalBytes);
        }
        memcpy(ptPixelDatasOut->aucPixelDatas,ptPixelDatasIn->aucPixelDatas,ptPixelDatasOut->iTotalBytes);

        return 0;
    }
    else if(iVideoBufOut->iPixelFormat == V4L2_PIX_FMT_RGB32)
    {
        ptPixelDatasOut->iHeight = ptPixelDatasIn->iHeight;
        ptPixelDatasOut->iWidth = ptPixelDatasIn->iWidth;
        ptPixelDatasOut->iBpp = 32;
        ptPixelDatasOut->iLineBytes = ptPixelDatasOut->iWidth * ptPixelDatasOut->iBpp / 8;
        ptPixelDatasOut->iTotalBytes = ptPixelDatasOut->iLineBytes * ptPixelDatasOut->iHeight;
        if(ptPixelDatasOut->aucPixelDatas == NULL)
        {
            ptPixelDatasOut->aucPixelDatas = (unsigned char *)malloc(ptPixelDatasOut->iTotalBytes);
        }

        for(y = 0;y < ptPixelDatasOut->iHeight;y++)
        {
            for(x = 0;x < ptPixelDatasOut->iWidth;x++)
            {
                color = *Ptr++;
                r = (color >> 11) & 0x1f;   //取出对应的颜色值
                g = (color >> 5) & 0x3f;
                b = color & 0x1f;
                r = r << 3;                 //由于前面取出后是低位，所以需要进行左移操作，将其变为高位
                g = g << 2;
                b = b << 3;
                *ptPixelDatasOut->aucPixelDatas++ = r;
                *ptPixelDatasOut->aucPixelDatas++ = g;
                *ptPixelDatasOut->aucPixelDatas++ = b;
                *ptPixelDatasOut->aucPixelDatas++ = 0;
            }
        }

        return 0;
    }

    return -1;
}


static int Rgb2RgbConvertExit(PT_VideoBuf iVideoBufOut)
{
    if(iVideoBufOut->tPixelDatas.aucPixelDatas)
    {
        free(iVideoBufOut->tPixelDatas.aucPixelDatas);
        iVideoBufOut->tPixelDatas.aucPixelDatas = NULL;
    }

    return 0;
}



static T_VideoConvert g_tRgb2RgbConvert = {
    .name        = "Rgb2Rgb",
    .isSupport   = Rgb2RgbisSupport,
    .Convert     = Rgb2RgbConvert,
    .ConvertExit = Rgb2RgbConvertExit,
};

/*注册*/

int Rgb2RgbInit(void)
{
    return RegisterVideoConvert(&g_tRgb2RgbConvert);;
}